package cn.hdu.liang.nio;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

public class PipeTest {
    public static void main(String[] args) throws IOException {
        Pipe pipe = Pipe.open();
        Pipe.SinkChannel sink = pipe.sink();
        ByteBuffer buffer = ByteBuffer.allocate(48);
        buffer.clear();
        buffer.put("hahaha".getBytes());
        buffer.flip();

        while(buffer.hasRemaining()) {
            sink.write(buffer);
        }

        Pipe.SourceChannel source = pipe.source();
        ByteBuffer buffer2 = ByteBuffer.allocate(48);
        source.read(buffer2);
        buffer2.flip();
        while(buffer2.hasRemaining()) {
            System.out.print((char) buffer2.get());
        }
        System.out.println();
    }
}